package handler

import (
	"sen-golang-study/go_game_study/hero_story_go_server/game_server/internal/interfaces/context"
)

// CmdHandlerFunc 命令处理器函数类型
type CmdHandlerFunc func(ctx context.CmdContext, msgBody []byte)

// CmdHandlerFactory 命令处理器工厂
type CmdHandlerFactory struct {
	handlers map[uint16]CmdHandlerFunc
}

// NewCmdHandlerFactory 创建命令处理器工厂
func NewCmdHandlerFactory() *CmdHandlerFactory {
	return &CmdHandlerFactory{
		handlers: make(map[uint16]CmdHandlerFunc),
	}
}

// RegisterHandler 注册命令处理器
func (f *CmdHandlerFactory) RegisterHandler(msgCode uint16, handler CmdHandlerFunc) {
	if handler != nil {
		f.handlers[msgCode] = handler
	}
}

// CreateHandler 创建命令处理器
func (f *CmdHandlerFactory) CreateHandler(msgCode uint16) CmdHandlerFunc {
	return f.handlers[msgCode]
}

// HasHandler 检查是否存在指定消息码的处理器
func (f *CmdHandlerFactory) HasHandler(msgCode uint16) bool {
	_, exists := f.handlers[msgCode]
	return exists
}

// GetAllMsgCodes 获取所有已注册的消息码
func (f *CmdHandlerFactory) GetAllMsgCodes() []uint16 {
	var msgCodes []uint16
	for msgCode := range f.handlers {
		msgCodes = append(msgCodes, msgCode)
	}
	return msgCodes
}